package com.ak.server.platform.controller;

import javax.validation.Valid;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ak.common.annotation.LogEndpoint;
import com.ak.common.controller.BaseController;
import com.ak.common.entity.AKResponse;
import com.ak.common.enums.Enums;
import com.ak.server.platform.entity.Dict;
import com.ak.server.platform.service.IDictService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author lihaodong
 * @since 2019-05-17
 */
@Api(tags = "字典模块")
@RestController
@RequestMapping("dict")
@RequiredArgsConstructor
public class DictController extends BaseController {

    private final IDictService dictService;

    /**
     * 添加字典信息
     *
     * @param dict
     * @return
     */
    @ApiOperation("添加字典信息")
    @LogEndpoint(operation = "添加字典信息")
    @PreAuthorize("hasAuthority('dict:add')")
    @PostMapping
    public AKResponse add(@Valid Dict dict) {
        dict.setCreateBy(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        return new AKResponse().data(dictService.save(dict));
    }

    /**
     * 获取字典列表集合
     *
     * @param page
     * @param dict
     * @return
     */
    @ApiOperation("查询字典集合")
    @LogEndpoint(operation = "查询字典集合")
    @GetMapping
    @PreAuthorize("hasAuthority('dict:view')")
    public AKResponse getList(Page<Dict> page, Dict dict) {
        dict.setDelFlag(Enums.DelFlag.EXIST.getCode());
        return new AKResponse().data(dictService.page(page, Wrappers.query(dict)));
    }


    /**
     * 更新字典
     *
     * @param dict
     * @return
     */
    @ApiOperation("更新字典")
    @LogEndpoint(operation = "更新字典")
    @PreAuthorize("hasAuthority('dict:update')")
    @PutMapping
    public AKResponse update(@Valid Dict dict) {
        dict.setUpdateBy(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        return new AKResponse().data(dictService.updateDict(dict));
    }


    /**
     * 根据id删除字典
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id删除字典")
    @LogEndpoint(operation = "根据id删除字典")
    @PreAuthorize("hasAuthority('dict:delete')")
    @DeleteMapping("{id}")
    public AKResponse delete(@PathVariable("id") String id) {
        return new AKResponse().data(dictService.removeById(id));
    }

}

